package br.com.acs.CameraIP.util;

import java.util.Map;

import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.MethodExpressionActionListener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FacesUtil {
	
	   
	public static String getRequestParameter(String name) {
		return (String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(name);
	}

	public static void exibirMensagemSucesso(String mensagem) {
		exibirMensagem(FacesMessage.SEVERITY_INFO, mensagem);
	}

	public static void exibirMensagemAlerta(String mensagem) {
		exibirMensagem(FacesMessage.SEVERITY_WARN, mensagem);
	}
	
	public static void exibirMensagemErro(String mensagem) {
		exibirMensagem(FacesMessage.SEVERITY_ERROR, mensagem);
	}
	
	private static void exibirMensagem(FacesMessage.Severity severity, String mensagem) {
		FacesMessage facesMessage = new FacesMessage(severity, "", mensagem);
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
	}

	public static ExternalContext getExternalContext() {
		return FacesContext.getCurrentInstance().getExternalContext();
	}
	
	@SuppressWarnings("rawtypes")
	public static Map getSessionMap() {
		return FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
	}
	
	public static ServletContext getServletContext() {
		return (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
	}
	
	public static HttpServletRequest getServletRequest() {
		return (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
	}
	
	public static HttpServletResponse getServletResponse() {
		return (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
	}
	public static MethodExpression createMethodExpression(String valueExpression, Class<?> expectedReturnType,           Class<?>[] expectedParamTypes) {
FacesContext fc = FacesContext.getCurrentInstance();
    ExpressionFactory factory = fc.getApplication().getExpressionFactory();

   return factory.createMethodExpression(fc.getELContext(), valueExpression, expectedReturnType, expectedParamTypes);
   }

   public static MethodExpressionActionListener createMethodActionListener(String valueExpression, Class<?> expectedReturnType,
                                 Class<?>[] expectedParamTypes) {
      return new MethodExpressionActionListener(createMethodExpression(valueExpression, expectedReturnType,
                             expectedParamTypes));
  }
   
   	@SuppressWarnings("rawtypes")
	public static Object getSessionAttribute(String attributeName) {    
		try {    
		    ExternalContext ec = getExternalContext();    
		    if (ec != null){    
		        Map attrMap = ec.getSessionMap();       
		        if (attrMap != null) {    
		            return attrMap.get(attributeName);    
		        } else {    
		            return null;    
		        }    
		    } else {    
		        return null;    
		    }    
		} catch (Exception e) {    
		    e.printStackTrace();    
		    return null;    
		}    
		}    
	
	@SuppressWarnings({ "unchecked", "rawtypes" })    
	public static void setSessionAttribute(String attributeName, Object attributeValue) {    
	try {    
	    ExternalContext ec = getExternalContext();    
	    if (ec != null){    
	        Map attrMap = ec.getSessionMap();       
	        if (attrMap != null) {    
	            attrMap.put(attributeName, attributeValue);    
	        }     
	    }     
	} catch (Exception e) {    
	    e.printStackTrace();    
	}    
	}  
}
